<?php
/**
 * Created by PhpStorm.
 * User: alexey
 * Date: 6/5/14
 * Time: 11:19 PM
 */

function generateFlyComment($parameters, $isOwner, $isSpammed, $inited)
{
  $profileButtons = "";

  $paramArray = null;
  if (!empty($parameters['quote'])) {
    $paramArray['author'] = $parameters['quote.author'] . ' написал:';
    $paramArray['quote'] = $parameters['quote'];
    $quote = \controller\HTMLTemplateUtil::generateHTML('comment-quote', $paramArray);
  }

  $paramArray = null;
  $paramArray['images'] = $parameters['images'];
  $images = \controller\HTMLTemplateUtil::generateHTML('comment-images', $paramArray);

  if ($inited) {
    $paramArray = null;
    $paramArray['button.link'] = $parameters['button.like']['link'];
    $paramArray['button.text'] = $parameters['button.like']['text'];
    $buttons = \controller\HTMLTemplateUtil::generateHTML('comment-button', $paramArray);

    $paramArray = null;
    $paramArray['button.link'] = $parameters['button.dislike']['link'];
    $paramArray['button.text'] = $parameters['button.dislike']['text'];
    $buttons .= \controller\HTMLTemplateUtil::generateHTML('comment-button', $paramArray);

    if (!$isSpammed && !$isOwner) {
      $paramArray = null;
      $paramArray['button.link'] = $parameters['button.spam']['link'];
      $paramArray['button.text'] = $parameters['button.spam']['text'];
      $buttons .= \controller\HTMLTemplateUtil::generateHTML('comment-button', $paramArray);
    }

    $paramArray = null;
    $paramArray['button.link'] = $parameters['button.reply']['link'];
    $paramArray['button.text'] = $parameters['button.reply']['text'];
    $buttons .= \controller\HTMLTemplateUtil::generateHTML('comment-reply-button', $paramArray);
    $paramArray = null;

    $paramArray['button.link'] = $parameters['button.profile']['link'];
    $paramArray['button.text'] = $parameters['button.profile']['text'];
    $profileButtons .= \controller\HTMLTemplateUtil::generateHTML('comment-button', $paramArray);
    $paramArray = null;

    $paramArray['button.link'] = $parameters['button.message']['link'];
    $paramArray['button.text'] = $parameters['button.message']['text'];
    $profileButtons .= \controller\HTMLTemplateUtil::generateHTML('comment-button', $paramArray);

  }

  $paramArray = null;
  $paramArray['comment.id'] = '#: ' . $parameters['comment.id'];
  $paramArray['userdiv.id'] = $parameters['comment.id'];
  $paramArray['comment.date'] = getMessageByKey('general.date') . $parameters['comment.date'];
  $paramArray['comment.time'] = getMessageByKey('general.time') . $parameters['comment.time'];
  $paramArray['user.image'] = $parameters['user.image'];
  $paramArray['user.registration'] = getMessageByKey('user.comment.registration') . $parameters['user.registration'];
  $paramArray['user.nick'] = getMessageByKey('user.comment.nick') . $parameters['user.nick'];
  $paramArray['user.email'] = getMessageByKey('user.comment.email') . $parameters['user.email'];
  $paramArray['user.messages'] = getMessageByKey('user.comment.messages') . $parameters['user.messages'];
  $paramArray['quote'] = $quote;
  $paramArray['images'] = $images;
  $paramArray['content'] = $parameters['content'];
  $paramArray['buttons'] = $buttons;
  $paramArray['user.buttons'] = $profileButtons;
  $paramArray['user.id'] = $parameters['user.id'];

  return \controller\HTMLTemplateUtil::generateHTML('comment', $paramArray);
}

function generateFlyPages($pagesNumber, $page, $flyId)
{
  $maxPages = 5;
  $paramArray = null;
  $pages = "";

  if ($page != 1) {
    $paramArray['page'] = "<";
    $paramArray['link'] = "location.href='?action=showfly&fid=" . $flyId . "&page=1'";
    $pages = $pages . \controller\HTMLTemplateUtil::generateHTML('page-button', $paramArray);
  }


  if ($pagesNumber <= $maxPages) {
    for ($i = 1; $i <= $pagesNumber; $i++) {
      $paramArray['page'] = $i;
      if ($i == $page) {
        $paramArray['link'] = "";
        $pages = $pages . \controller\HTMLTemplateUtil::generateHTML('page-selected-button', $paramArray);
      } else {
        $paramArray['link'] = "location.href='?action=showfly&fid=" . $flyId . "&page=" . $i . "'";
        $pages = $pages . \controller\HTMLTemplateUtil::generateHTML('page-button', $paramArray);
      }
    }

  } else if ($page + 2 <= $pagesNumber) {


    if ($page - 2 >= 1) {
      $start = $page - 2;
      $end = $page + 2;
    } else if ($pagesNumber > $maxPages) {
      $start = 1;
      $end = $maxPages;
    } else {
      $start = 1;
      $end = $pagesNumber;
    }

    for ($i = $start; $i <= $end; $i++) {
      $paramArray['page'] = $i;
      if ($i == $page) {
        $paramArray['link'] = "";
        $pages = $pages . \controller\HTMLTemplateUtil::generateHTML('page-selected-button', $paramArray);
      } else {
        $paramArray['link'] = "location.href='?action=showfly&fid=" . $flyId . "&page=" . $i . "'";
        $pages = $pages . \controller\HTMLTemplateUtil::generateHTML('page-button', $paramArray);
      }
    }

  } else {

    for ($i = $pagesNumber - $maxPages + 1; $i <= $pagesNumber; $i++) {
      $paramArray['page'] = $i;
      if ($i == $page) {
        $paramArray['link'] = "";
        $pages = $pages . \controller\HTMLTemplateUtil::generateHTML('page-selected-button', $paramArray);
      } else {
        $paramArray['link'] = "location.href='?action=showfly&fid=" . $flyId . "&page=" . $i . "'";
        $pages = $pages . \controller\HTMLTemplateUtil::generateHTML('page-button', $paramArray);
      }
    }

  }

  if ($page != $pagesNumber) {
    $paramArray['page'] = ">";
    $paramArray['link'] = "location.href='?action=showfly&fid=" . $flyId . "&page=" . $pagesNumber . "'";
    $pages = $pages . \controller\HTMLTemplateUtil::generateHTML('page-button', $paramArray);
  }

  $paramArray = null;
  $paramArray['pages'] = $pages;
  echo \controller\HTMLTemplateUtil::generateHTML('pages', $paramArray);
}

function generateTopicPages($pagesNumber, $page, $topicId)
{
  $maxPages = 5;
  $paramArray = null;
  $pages = "";

  if ($page != 1) {
    $paramArray['page'] = "<";
    $paramArray['link'] = "location.href='?action=topic&tid=" . $topicId . "&page=1'";
    $pages = $pages . \controller\HTMLTemplateUtil::generateHTML('page-button', $paramArray);
  }


  if ($pagesNumber <= $maxPages) {
    for ($i = 1; $i <= $pagesNumber; $i++) {
      $paramArray['page'] = $i;
      if ($i == $page) {
        $paramArray['link'] = "";
        $pages = $pages . \controller\HTMLTemplateUtil::generateHTML('page-selected-button', $paramArray);
      } else {
        $paramArray['link'] = "location.href='?action=topic&tid=" . $topicId . "&page=" . $i . "'";
        $pages = $pages . \controller\HTMLTemplateUtil::generateHTML('page-button', $paramArray);
      }
    }

  } else if ($page + 2 <= $pagesNumber) {

    if ($page - 2 >= 1) {
      $start = $page - 2;
      $end = $page + 2;
    } else if ($pagesNumber > $maxPages) {
      $start = 1;
      $end = $maxPages;
    } else {
      $start = 1;
      $end = $pagesNumber;
    }

    for ($i = $start; $i <= $end; $i++) {
      $paramArray['page'] = $i;
      if ($i == $page) {
        $paramArray['link'] = "";
        $pages = $pages . \controller\HTMLTemplateUtil::generateHTML('page-selected-button', $paramArray);
      } else {
        $paramArray['link'] = "location.href='?action=topic&tid=" . $topicId . "&page=" . $i . "'";
        $pages = $pages . \controller\HTMLTemplateUtil::generateHTML('page-button', $paramArray);
      }
    }

  } else {

    for ($i = $pagesNumber - $maxPages + 1; $i <= $pagesNumber; $i++) {
      $paramArray['page'] = $i;
      if ($i == $page) {
        $paramArray['link'] = "";
        $pages = $pages . \controller\HTMLTemplateUtil::generateHTML('page-selected-button', $paramArray);
      } else {
        $paramArray['link'] = "location.href='?action=topic&tid=" . $topicId . "&page=" . $i . "'";
        $pages = $pages . \controller\HTMLTemplateUtil::generateHTML('page-button', $paramArray);
      }
    }

  }

  if ($page != $pagesNumber) {
    $paramArray['page'] = ">";
    $paramArray['link'] = "location.href='?action=topic&tid=" . $topicId . "&page=" . $pagesNumber . "'";
    $pages = $pages . \controller\HTMLTemplateUtil::generateHTML('page-button', $paramArray);
  }

  $paramArray = null;
  $paramArray['pages'] = $pages;
  echo \controller\HTMLTemplateUtil::generateHTML('pages', $paramArray);
}

function generateTopicComment($parameters, $isOwner, $isSpammed, $inited)
{
  $profileButtons = "";
  $deleteButtons = "";

  $paramArray = null;
  if (!empty($parameters['quote'])) {
    $paramArray['author'] = $parameters['quote.author'] . ' написал:';
    $paramArray['quote'] = $parameters['quote'];
    $quote = \controller\HTMLTemplateUtil::generateHTML('comment-quote', $paramArray);
  }

  $paramArray = null;
  $paramArray['images'] = $parameters['images'];
  $images = \controller\HTMLTemplateUtil::generateHTML('comment-images', $paramArray);

  $buttons = '';

  if ($inited) {
    $paramArray = null;
    $paramArray['button.link'] = $parameters['button.like']['link'];
    $paramArray['button.text'] = $parameters['button.like']['text'];
    $buttons .= \controller\HTMLTemplateUtil::generateHTML('comment-button', $paramArray);

    $paramArray = null;
    $paramArray['button.link'] = $parameters['button.dislike']['link'];
    $paramArray['button.text'] = $parameters['button.dislike']['text'];
    $buttons .= \controller\HTMLTemplateUtil::generateHTML('comment-button', $paramArray);

    if (!$isSpammed && !$isOwner) {
      $paramArray = null;
      $paramArray['button.link'] = $parameters['button.spam']['link'];
      $paramArray['button.text'] = $parameters['button.spam']['text'];
      $buttons .= \controller\HTMLTemplateUtil::generateHTML('comment-button', $paramArray);
    } else if ($isOwner) {
      $editButtons = null;
      $paramArray = null;
      $paramArray['button.link'] = $parameters['button.edit.comment']['link'];
      $paramArray['button.text'] = $parameters['button.edit.comment']['text'];
      $editButtons .= \controller\HTMLTemplateUtil::generateHTML('comment-button', $paramArray);
      $paramArray = null;
      $paramArray['button.link'] = $parameters['button.edit.comment.cancel']['link'];
      $paramArray['button.text'] = $parameters['button.edit.comment.cancel']['text'];
      $editButtons .= \controller\HTMLTemplateUtil::generateHTML('comment-button', $paramArray);

      $paramArray = null;
      $paramArray['content'] = $parameters['content'];
      $paramArray['edit.buttons'] = $editButtons;
      $paramArray['i'] = $parameters['i'];
      $paramArray['input.hidden'] = $parameters['input.hidden'];
      $paramArray['edit.action'] = $parameters['edit.action'];
      $editDiv = \controller\HTMLTemplateUtil::generateHTML('comment-edit', $paramArray);

      $paramArray = null;
      $paramArray['button.link'] = $parameters['button.edit']['link'];
      $paramArray['button.text'] = $parameters['button.edit']['text'];
      $buttons .= \controller\HTMLTemplateUtil::generateHTML('comment-button', $paramArray);

      $paramArray = null;
      $paramArray['button.link'] = $parameters['button.delete']['link'];
      $paramArray['button.text'] = $parameters['button.delete']['text'];
      $buttons .= \controller\HTMLTemplateUtil::generateHTML('comment-button', $paramArray);

      $paramArray = null;
      $deleteButtons = "";
      $paramArray['button.link'] = $parameters['button.delete.ok']['link'];
      $paramArray['button.text'] = $parameters['button.delete.ok']['text'];
      $deleteButtons .= \controller\HTMLTemplateUtil::generateHTML('comment-button', $paramArray);

      $paramArray['button.link'] = $parameters['button.delete.cancel']['link'];
      $paramArray['button.text'] = $parameters['button.delete.cancel']['text'];
      $deleteButtons .= \controller\HTMLTemplateUtil::generateHTML('comment-button', $paramArray);

      $paramArray = null;
      $paramArray['i'] = $parameters['i'];
      $paramArray['delete.buttons'] = $deleteButtons;
      $paramArray['delete.text'] = $parameters['delete.text'];
      $deleteDiv = \controller\HTMLTemplateUtil::generateHTML('comment-delete-div', $paramArray);
    }

    $paramArray = null;
    $paramArray['button.link'] = $parameters['button.reply']['link'];
    $paramArray['button.text'] = $parameters['button.reply']['text'];
    $buttons .= \controller\HTMLTemplateUtil::generateHTML('comment-reply-button', $paramArray);

    $paramArray = null;

    $paramArray['button.link'] = $parameters['button.profile']['link'];
    $paramArray['button.text'] = $parameters['button.profile']['text'];
    $profileButtons .= \controller\HTMLTemplateUtil::generateHTML('comment-button', $paramArray);

    $paramArray = null;
    $paramArray['button.link'] = $parameters['button.message']['link'];
    $paramArray['button.text'] = $parameters['button.message']['text'];
    $profileButtons .= \controller\HTMLTemplateUtil::generateHTML('comment-button', $paramArray);

  }

  $paramArray = null;
  $paramArray['button.link'] = $parameters['button.facebook']['link'];
  $paramArray['button.text'] = $parameters['button.facebook']['text'];
  $buttons .= \controller\HTMLTemplateUtil::generateHTML('comment-reply-button', $paramArray);

  $paramArray = null;
  $paramArray['comment.id'] = '#: ' . $parameters['comment.id'];
  $paramArray['facebook'] = $parameters['facebook'];
  $paramArray['userdiv.id'] = $parameters['comment.id'];
  $paramArray['comment.date'] = getMessageByKey('general.date') . $parameters['comment.date'];
  $paramArray['comment.time'] = getMessageByKey('general.time') . $parameters['comment.time'];
  $paramArray['user.image'] = $parameters['user.image'];
  $paramArray['user.registration'] = getMessageByKey('user.comment.registration') . $parameters['user.registration'];
  $paramArray['user.nick'] = getMessageByKey('user.comment.nick') . $parameters['user.nick'];
  $paramArray['user.email'] = getMessageByKey('user.comment.email') . $parameters['user.email'];
  $paramArray['user.messages'] = getMessageByKey('user.comment.messages') . $parameters['user.messages'];
  $paramArray['quote'] = $quote;
  $paramArray['images'] = $images;
  $paramArray['content'] = $parameters['content'];
  $paramArray['buttons'] = $buttons;
  $paramArray['user.buttons'] = $profileButtons;
  $paramArray['i'] = $parameters['i'];
  $paramArray['user.id'] = $parameters['user.id'];
  $paramArray['comment.edit'] = $editDiv;
  $paramArray['comment.delete'] = $deleteDiv;

  return \controller\HTMLTemplateUtil::generateHTML('comment', $paramArray);
}